<template>
  <div class="app-container">
      <el-form ref="bankcardForm" :rules="rules" :model="bankcardForm" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">

        <el-form-item label="银行卡号" prop="bankcardCode">
          <el-input v-model="bankcardForm.bankcardCode" />
        </el-form-item>
        <el-form-item label="开户银行" prop="bankID">
          <el-select v-model="bankcardForm.bankID" filterable remote :remote-method="loadBankList" placeholder="请选择">
            <el-option
              v-for="item in bankOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="开户责任人" prop="employeeID">
          <el-input v-model="bankcardForm.employeeID" />
        </el-form-item>
        <el-form-item label="余额" prop="balance">
          <el-input v-model="bankcardForm.balance" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="bankcardForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
        </el-form-item>
      </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="saveBankcard()">
        确认
      </el-button>
      <el-button v-if="bankcardForm.status.value === 5" type="danger" @click="handleDelete(bankcardForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import {  saveBankcard, getBankcardDetail, removeBankcardByID } from '@/api/finance/bankcard'
import { getBankUsableList } from '@/api/finance/bank'


import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'

import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      bankOptions:[],
      bankcardForm: {
        ID: undefined,
        remark: '',
        bankcard_ID: 0,
        title: '',
        status: {
          value:1
        }
      },
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.loadBankList();
    let bankcardID = this.$route.query && this.$route.query.bankcardID
    if (bankcardID !== undefined) {
      bankcardID = parseInt(bankcardID)
      if (!isNaN(bankcardID)) {
        this.listLoading = true
        getBankcardDetail({
          bankcardID:bankcardID
        }).then(response => {
          if (response.status === 200) {
            this.bankcardForm = response.data
            console.log(this.bankcardForm)
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'bankcard' })
    },
    loadBankList:function(bankName){
      let formData = {}
      if(bankName != undefined){
          formData['keyword'] = bankName
      }
      getBankUsableList(formData).then(res=>{
        if(res.status == 200){
          this.bankOptions = res.data.list
        }
      })
    },
    saveBankcard() {
      let _this = this
      this.$refs['bankcardForm'].validate((valid) => {
        if (valid) {
          try {
            _this.bankcardForm.status = _this.bankcardForm.status.value;
          } catch (error) {
            _this.bankcardForm.status = 6;
          }
          if(_this.bankcardForm.hasOwnProperty('ID')){
            if(_this.bankcardForm.ID !== undefined){
              _this.bankcardForm.bankcardID = _this.bankcardForm.ID;
            }

          }
          saveBankcard(_this.bankcardForm).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'bankcard' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
          })
        }
      })
    },
    handleDelete(row, index) {
      removeBankcardByID(row.ID).then(() => {
        this.$notify({
          title: 'Success',
          message: 'Update Successfully',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    }
  }
}
</script>
